package org.yn.base.util;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class SMSUtils {

	@Value("${aliyun.sms.signName}")
	private  String signName;

	@Value("${aliyun.sms.templateCode}")
	private  String templateCode;

	@Value("${aliyun.sms.regionId}")
	private  String regionId;


	@Value("${aliyun.sms.accessKeyId}")
	private  String accessKeyId;

	@Value("${aliyun.sms.accessKeySecret}")
	private  String accessKeySecret;
	/**
	 * 发送短信

	 * @param phoneNumbers 手机号
	 * @param param 参数
	 */
	public  void sendMessage(String phoneNumbers,String param){
		DefaultProfile profile = DefaultProfile.getProfile(this.regionId, this.accessKeyId, this.accessKeySecret);
		IAcsClient client = new DefaultAcsClient(profile);
 
		SendSmsRequest request = new SendSmsRequest();
		request.setSysRegionId(this.regionId);
		request.setPhoneNumbers(phoneNumbers);
		request.setSignName(this.signName);
		request.setTemplateCode(this.templateCode);
		request.setTemplateParam("{\"code\":\""+param+"\"}");
		try {
			SendSmsResponse response = client.getAcsResponse(request);
			System.out.println("短信发送成功");
		}catch (ClientException e) {
			e.printStackTrace();
		}
	}
}